﻿/****************************************************************************************
*	Mouzetrap - client/server framework	
*
*	$HeadURL: http://mouzetrap.googlecode.com/svn/trunk/Mouzetrap/Network/InboundPacket.cs $
*	$Id: InboundPacket.cs 11 2009-06-11 09:10:25Z tobz@fallenrealms.net $
* 
*	Copyright (c) 2008 Toby Lawrence
* 
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
* 
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
* 
****************************************************************************************/

using System;
using System.IO;

namespace Mouzetrap.Network
{
	/// <summary>
	/// Provides the base for an incoming packet wrapper.
	/// </summary>
	public class InboundPacket : BinaryReader, IPacket
	{
		/// <summary>
		/// Creates a new <see cref="InboundPacket" /> instance for the given buffer.
		/// </summary>
		/// <param name="buffer">the buffer to read from</param>
		/// <param name="offset">the offset in the buffer to start reading from</param>
		/// <param name="length">the length of data to read</param>
		public InboundPacket(byte[] buffer, int offset, int length)
			: base(new MemoryStream(buffer, offset, length), NetworkHelper.DefaultEncoding)
		{
		}

		/// <summary>
		/// Creates a new <see cref="InboundPacket" /> instance for the given buffer.
		/// </summary>
		/// <param name="buffer">the buffer to read from</param>
		/// <param name="offset">the offset in the buffer to start reading from</param>
		/// <param name="length">the length of data to read</param>
		public InboundPacket(ArraySegment<byte> buffer, int offset, int length)
			: base(new MemoryStream(buffer.Array, buffer.Offset + offset, length), NetworkHelper.DefaultEncoding)
		{
		}

		#region IPacket Members

		/// <summary>
		/// The ID of the packet.
		/// </summary>
		/// <remarks>This is most typically a unique number that identifies a specific type of packet.</remarks>
		public int PacketId { get; private set; }

		/// <summary>
		/// The direction of the packet. (client to server, server to client)
		/// </summary>
		public PacketDirection Direction
		{
			get { return PacketDirection.Inbound; }
		}

		/// <summary>
		/// Gets all the data from the packet.
		/// </summary>
		/// <returns>a byte array of the data</returns>
		public byte[] GetPacketData()
		{
			return ((MemoryStream) BaseStream).ToArray();
		}

		#endregion
	}
}